package cn.com.customer.manager.ui.customer.detail

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import cn.com.customer.manager.Constants.Companion.BASE_IMAGE_URL
import cn.com.customer.manager.R
import cn.com.customer.manager.adapter.FlowTagAdapter
import cn.com.customer.manager.base.BaseActivity
import cn.com.customer.manager.base.BaseApplication
import cn.com.customer.manager.bean.CustomerInfo
import cn.com.customer.manager.manager.TokenManager
import cn.com.customer.manager.ui.order.addOrder.AddOrderActivity
import cn.com.customer.manager.ui.task.addTask.AddTaskActivity
import com.xuexiang.xui.widget.flowlayout.FlowTagLayout
import com.xuexiang.xui.widget.imageview.strategy.impl.GlideImageLoadStrategy
import kotlinx.android.synthetic.main.activity_customer_detail.*
import kotlinx.android.synthetic.main.menu_statelayout_fab.*

/**
 * ============================================
 * 文件名：客户详情  // TODO 客户分组以及标签显示
 * ============================================
 */
class CustomerDetailActivity : BaseActivity(), ViewPager.OnPageChangeListener,CustomerDetailContract.View{
   var mPresenter:CustomerDetailContract.Presenter=CustomerDetailPresenter(this)
    private var params: MutableMap<String, String> = mutableMapOf()
    private lateinit var  id:String
    private lateinit var name:String

    private val fragments: ArrayList<Fragment> = ArrayList(4)

    /**
     * 获取layout资源
     */
    override fun getLayoutId(): Int = R.layout.activity_customer_detail

    /**
     * 初始化控件
     */
    override fun initView(savedInstanceState: Bundle?) {
        initActionBar("客户详情")
        params["token"] = TokenManager.getInstance().token
        params["customerId"] = intent.getStringExtra("customerId")
        mPresenter.getData(params)
        fab_add_order.setOnClickListener(this)
        fab_add_task.setOnClickListener(this)

    }


    override fun onPageScrollStateChanged(state: Int) {
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
    }

    override fun onPageSelected(position: Int) {

    }

    override fun setData(data: CustomerInfo) {
        id=data.id
        name=data.name
        if (!data.headUrl.isNullOrEmpty()){
        GlideImageLoadStrategy().loadImage(iv_photo,BASE_IMAGE_URL+data.headUrl) }
        tv_name.text=data.name
        tv_role.text=data.duty
        tv_phone.text=data.mobile
        tv_message.text=data.email
        tv_address.text=data.address
        if(data.groups.isNotEmpty()){
        tv_customer_group.text=data.groups[0].groupName
        }
        tv_birthday.text=data.birthday
        tv_phone_fixe.text=data.telephone
        tv_chuanzhen.text = data.fax
        tv_remark.text=data.remark
        tv_address_company.text=data.country+data.province+data.city
        val tagAdapter = FlowTagAdapter(BaseApplication.getContext())
        ft.adapter = tagAdapter
        ft.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_NONE)
        tagAdapter.addTags(data.tags)
    }


    override fun setPresenter(presenter: CustomerDetailContract.Presenter) {
        mPresenter=presenter
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when(v?.id){
            R.id.fab_add_order ->{
                startActivity(Intent(this, AddOrderActivity::class.java).putExtra("customerId", id).putExtra("name",name))
            }
            R.id.fab_add_task ->{
                startActivity(Intent(this, AddTaskActivity::class.java).putExtra("customerId", id).putExtra("name",name))
            }
        }
    }
}
